﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using TIS.DAO;
using System.Web.UI.WebControls;

namespace TIS.Administrator
{
    public partial class AddNews : System.Web.UI.Page
    {
        LanguageDAO _languageDao = new LanguageDAO();
        CategoryDAO _categoryDao = new CategoryDAO();
        NewsDAO _newsDao = new NewsDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["langId"] == null)
                {
                    Session["langId"] = 1;
                }
                GetDataDdlLanguage();
                loadForm();
            }
        }


        public void GetDataDdlLanguage()
        {
            DataTable dtLanguage = _languageDao.GetAllLanguage();
            if (dtLanguage.Rows.Count > 0)
            {
                ddlLanguage.DataSource = dtLanguage;
                ddlLanguage.DataTextField = "LanguageName";
                ddlLanguage.DataValueField = "LanguageId";
                //DataRow emtyLanguage = dtLanguage.NewRow();
                //emtyLanguage["LanguageName"] = "Chọn ngôn ngữ bài viết";
                //emtyLanguage["LanguageId"] = "0";
                //dtLanguage.Rows.InsertAt(emtyLanguage, 0);
                ddlLanguage.DataBind();
                ddlLanguage.SelectedValue = Session["langId"].ToString();

                DataTable dtCategory = _categoryDao.ListCategory(int.Parse(ddlLanguage.SelectedValue));

                if (dtCategory.Rows.Count > 0)
                {
                    ddlCategory.DataSource = dtCategory;
                    ddlCategory.DataTextField = "CategoryName";
                    ddlCategory.DataValueField = "CategoryId";
                    ddlCategory.DataBind();
                }
            }
        }
        public void loadForm()
        {
            if (Request.QueryString["id"] != null)
            {
                string id = Request.QueryString["id"].ToString();
                DataTable dt = _newsDao.GetNewsById(int.Parse(id));
                foreach (DataRow row in dt.Rows)
                {
                    txtTitle.Text = row["NewsTitle"].ToString();
                    txtDescription.Text = row["NewsContent"].ToString();
                    fckContent.Value = row["NewsDescription"].ToString();
                    txtMetaTitle.Text = row["NewsMetaTitle"].ToString();
                    txtMetaDescription.Text = row["NewsMetaDescription"].ToString();
                    txtMetaKeyword.Text = row["NewsMetaKeyword"].ToString();
                    txtSEOUrl.Text = row["NewsSEOUrl"].ToString();
                    ddlCategory.SelectedValue = row["CategoryId"].ToString();

                    if (row["NewsStatus"].ToString().Equals("1"))
                    {
                        cbDisplay.Checked = true;
                        cbHide.Checked = false;
                    }
                    else
                    {
                        cbDisplay.Checked = false;
                        cbHide.Checked = true;
                    }
                }


            }
        }
       

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            string title = txtTitle.Text;
            string content = txtDescription.Text;
            string description = fckContent.Value;
            string metaTitle = txtMetaTitle.Text == "" ? title : txtMetaTitle.Text;
            string metaDescription = txtMetaDescription.Text == "" ? content : txtMetaDescription.Text;
            string meteKeyword = txtMetaKeyword.Text == "" ? title : txtMetaKeyword.Text;
            string seoUrl = txtSEOUrl.Text == "" ? URLDecode.ConvertToUnSign(title) : txtSEOUrl.Text;
            int categoryId = int.Parse(ddlCategory.SelectedValue);
            int status;
            string imgSmall = "";
            int check;
            int langId = int.Parse(Session["langId"].ToString());
            if (cbDisplay.Checked)
            {
                status = 1;
            }
            else
            {
                status = 0;
            }

            if (ckbDateTime.Checked)
            {
                check = 1;
            }
            else
            {
                check = 0;
            }

            if (Request.QueryString["id"] == null)
            {
                if (fileUpload.FileName != "")
                {
                    imgSmall = "/Upload/Image/" + fileUpload.FileName;
                    fileUpload.SaveAs(Server.MapPath("~/Upload/Image/") + fileUpload.FileName);
                }

                _newsDao.InsertNews(title,categoryId,imgSmall,content,description,metaTitle,metaDescription,meteKeyword,seoUrl,status,langId);

                Response.Redirect("ManagerNews.aspx");
            }
            else
            {
                string id = Request.QueryString["id"].ToString();
                if (fileUpload.FileName != "")
                {
                    imgSmall = "/Upload/Image/" + fileUpload.FileName;
                    fileUpload.SaveAs(Server.MapPath("~/Upload/Image/") + fileUpload.FileName);
                }
                _newsDao.UpdateNews(int.Parse(id), title, categoryId, imgSmall, content, description, metaTitle, metaDescription, meteKeyword, seoUrl, status, langId, check);
                Response.Redirect("ManagerNews.aspx");
            }
                
        }

        protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["langId"] = ddlLanguage.SelectedValue;
            Response.Redirect("AddNews.aspx");
        }

        

    }
}